package it.polimi.digitalstate.webbeans;

import it.polimi.digitalstate.dto.JoinVoteDTO;
import it.polimi.digitalstate.dto.JoinrequestDTO;
import it.polimi.digitalstate.manager.AuthenticationManagerLocal;
import it.polimi.digitalstate.manager.MembershipManagerLocal;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import managerexception.CannotSendJoinVote;

@ManagedBean(name="joinVoteBean")
@ViewScoped
public class JoinVoteBean {
	@EJB
	AuthenticationManagerLocal ejb_user;
	@EJB
	MembershipManagerLocal ejb_memmanager;
	private JoinVoteDTO joinvote;
	private JoinrequestDTO joinrequest;
	private String jrid;
	
	public String getJrid() {
		return jrid;
	}
	public void setJrid(String jrid) {
		this.jrid = jrid;
	}

	@PostConstruct
	public void init(){
//		System.out.println("initiazlie joinvote: " + jrid);
		if (jrid != null)
		{
			joinrequest = ejb_memmanager.getMembershipRequest(Integer.parseInt(jrid));
			joinvote = new JoinVoteDTO (ejb_user.getLoggedCitizen(), joinrequest);
		}	
	}

	public String accept() {
		joinvote.setApproval(true);
		try {
			ejb_memmanager.submitJoinVote(joinvote);
		} catch (CannotSendJoinVote e) {
			return "joinvoteerror?faces-redirect=true";
		}
		return "joinvotesubmitted?faces-redirect=true";
	}
	
	public String ignore() {
		joinvote.setApproval(false);
		try {
			ejb_memmanager.submitJoinVote(joinvote);
		} catch (CannotSendJoinVote e) {
			return "joinvoteerror?faces-redirect=true";
		}
		return "joinvotesubmitted?faces-redirect=true";
	}
	
	public JoinrequestDTO getJoinrequest() {
		return joinrequest;
	}


	public void setJoinrequest(JoinrequestDTO joinrequest) {
		this.joinrequest = joinrequest;
	}

}

